var globalData = {
  VERSION: 'v1.0.0',
  //HOST: "http://localhost:9000/",
  HOST: "https://www.putstore.cn/",
  PROFILE_RECOVER_FLAG: 'http://www.putstore.cn',
  userInfo : { },
  onLogin : null,
};
var appConfig = {
  globalData: globalData,
  // 初始化App
  onLaunch: function() {
    let user = wx.getStorageSync('userInfo')
    if (user && user.id) { 
      this.globalData.userInfo = user;
    }else{
      this.globalData.userInfo = {}
    }
    if (appConfig.globalData.userInfo && appConfig.globalData.userInfo.id) {
      appConfig.GET('api/user/info?user=' + appConfig.globalData.userInfo.id, {}, function (res) {
        if (res && res.data) {
          appConfig.setGlobalData('userInfo', res.data)
        }
      })
    }
  },
  // 返回上一页面
  btnBack: function() {
    wx.navigateBack()
  },
  setGlobalData : function(name, value){
    appConfig.globalData[name] = value
    wx.setStorageSync(name, value)
  },
  refreshUserInfo: function (cb, fail) {
    if (cb && appConfig.globalData.userInfo && appConfig.globalData.userInfo.id) {
      appConfig.GET('api/user/info?user=' + appConfig.globalData.userInfo.id, {}, function (res) {
        if (res && res.data) {
          appConfig.setGlobalData('userInfo', res.data)
          cb && cb(appConfig.globalData.userInfo)
        } else {
          fail && fail()
        }
      })
    } else {
      fail && fail()
    }
  },
  getUserInfo: function (cb, fail) {
    if (cb && appConfig.globalData.userInfo && appConfig.globalData.userInfo.id){
      cb && cb(appConfig.globalData.userInfo)
    } else {
      fail && fail()
    }
  },
  getUserInfoAndLogin: function (cb, fail) {
    if (appConfig.globalData.userInfo && appConfig.globalData.userInfo.id) {
      appConfig.GET('api/user/info?user=' + appConfig.globalData.userInfo.id, {}, function (res) {
        if (res && res.data) {
          appConfig.setGlobalData('userInfo', res.data)
          cb && cb(appConfig.globalData.userInfo)
        }else{
          fail && fail()
        }
      })
    } else {
      appConfig.globalData.onLogin = function(user){
        if (user) {
          appConfig.setGlobalData('userInfo', user)
          cb && cb(user)
        } else {
          fail && fail()
        }
        appConfig.globalData.onLogin = null
      }
      wx.navigateTo({ url: '/pages/login/login' })
    }
  },
  /**
   * get请求
   */
  GET: function (url, data, success, fail, complete) {
    wx.showNavigationBarLoading();
    // 构造请求参数
    data = data || {};
    wx.request({
      url: globalData.HOST + url,
      header: { 'content-type': 'application/json'},
      data: data,
      dataType: 'json',
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.success) {
            success && success(res.data);
          } else {
            appConfig.showError(res.data ? res.data.errMsg : '服务器错误', function () { fail && fail(res); });
          }
        } else {
          appConfig.showError(res.errMsg, function () { fail && fail(res); });
        }
      },
      fail: function (res) {
        appConfig.showError(res.errMsg, function () { fail && fail(res); });
      },
      complete: function (res) {
        wx.hideNavigationBarLoading();
        complete && complete(res);
      },
    });
  },
  /**
   * post提交
   */
  POST: function (url, data, success, fail, complete) {
    wx.showNavigationBarLoading();
    data = data || {}
    wx.request({
      url: globalData.HOST + url,
      header: { 'content-type': 'application/json' },
      method: 'POST',
      data: data,
      dataType : 'json',
      success: function (res) {
        if(res.statusCode == 200){
          if (res.data.success){
            success && success(res.data);
          }else{
            appConfig.showError(res.data ? res.data.errMsg : '服务器错误', function () { fail && fail(res); });
          }
        }else{
          appConfig.showError(res.errMsg, function () { fail && fail(res); });
        }
      },
      fail: function (res) {
        appConfig.showError(res.errMsg, function () { fail && fail(res); });
      },
      complete: function (res) {
        wx.hideLoading()
        wx.hideNavigationBarLoading()
        complete && complete(res)
      }
    });
  },
  /**
 * 显示成功提示框
 */
  showSuccess: function (msg, callback) {
    wx.showModal({
      title: '友情提示',
      content: msg,
      icon: 'success',
      showCancel: false,
      success: function (res) {
        callback && callback();
      }
    });
  },
  /**
   * 显示失败提示框
   */
  showError: function (msg, callback) {
    wx.showModal({
      title: '友情提示',
      content: msg,
      showCancel: false,
      success: function (res) {
        callback && callback();
      }
    });
  },
}
App(appConfig)